﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class ry_BLL
    {
        //创建数据对象
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 获取数据源
        /// </summary>
        public static object GetSource()
        {
            return from p in db.ZG select new { p.zgId, p.zgName, p.zgPwd,zgSex=(p.zgSex==true?"男":"女"), bWork=(p.bWork==true?"是":"否"),bMana=( p.bMana==true?"是":"否") };
        }

        /// <summary>
        /// 添加职工
        /// </summary>
        /// <param name="newZG">新的职工数据对象</param>
        public static bool Add(ZG newZG)
        {
            //数据更新成功标记
            bool bl = false;
            try {
                //排除重复项
                if (db.ZG.Single(p => p.zgName == newZG.zgName).zgId > -1)
                    return false;
            }
            catch { }
            //准备数据
            db.ZG.InsertOnSubmit(newZG);
            //提交更新
            db.SubmitChanges();
            bl = true;
            return bl;
        }

        /// <summary>
        /// 删除数据项
        /// </summary>
        /// <param name="zgId">要删除的职工标识</param>
        public static bool Delete(string zgId)
        {
            db = new dbExamDataContext();
            //数据更新成功标记
            bool bl = false;
            try
            {
                //找到要删除的数据项
                ZG zg = db.ZG.Single(p => p.zgId == decimal.Parse(zgId));
                //准备数据
                db.ZG.DeleteOnSubmit(zg);
                //提交更新
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="zgId">要更新的数据标识</param>
        /// <param name="newZG">新数据对象</param>
        public static bool Update(string zgId, ZG newZG)
        {
            //数据更新成功标记
            bool bl = false;
            try
            {
                //找到需要更新的数据项
                ZG zg = db.ZG.Single(p => p.zgId == decimal.Parse(zgId));
                //重新指定值
                zg.zgName = newZG.zgName;
                zg.zgPwd = newZG.zgPwd;
                zg.zgSex = newZG.zgSex;
                zg.bMana = newZG.bMana;
                zg.bWork = newZG.bWork;
                //提交更新
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
